//
//  SRHomeworkPictureHeaderView.m
//  StoryShow
//
//  Created by CyonLeuMBP on 2017/9/11.
//  Copyright © 2017年 qudiandu.cn. All rights reserved.
//

#import "SRHomeworkPictureHeaderView.h"
#import <FZIconImageView.h>
#import <NSDate+DateToDisplayString.h>
#import <NSAttributedString+Height.h>
#import <NSString+Size.h>

@interface SRHomeworkPictureHeaderView ()

@property (weak, nonatomic) IBOutlet FZIconImageView *teacherIconImageView;
@property (weak, nonatomic) IBOutlet UILabel *teacherNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *createTimeLabel;

@property (weak, nonatomic) IBOutlet UIImageView *pageImageView;


@property (weak, nonatomic) IBOutlet UILabel *titleLabel;

@property (weak, nonatomic) IBOutlet UIView *audioContentView;
@property (weak, nonatomic) IBOutlet UIImageView *playImageView;
@property (weak, nonatomic) IBOutlet UILabel *timeLengthLabel;
@property (strong, nonatomic) UIActivityIndicatorView * activityView;

@end

@implementation SRHomeworkPictureHeaderView

- (void)awakeFromNib {
    [super awakeFromNib];
    
    FZStyleSheet *css = [FZStyleSheet currentStyleSheet];
    self.teacherIconImageView.iconStyle = FZIconImageViewStyleCircle;
    self.teacherNameLabel.textColor = self.createTimeLabel.textColor = css.color_2;
    self.teacherNameLabel.font = css.fontOfH5;
    self.createTimeLabel.font = css.fontOfH7;

    self.pageImageView.layer.cornerRadius = 5;
    self.pageImageView.clipsToBounds = YES;
    
    self.titleLabel.textColor = css.color_2;
    self.titleLabel.font = css.fontOfH5;
    
    self.audioContentView.clipsToBounds = YES;
    self.audioContentView.layer.cornerRadius = 4;
    self.audioContentView.backgroundColor = [UIColor colorWithHex:0x10aa14];
    
    WEAKSELF
    [self.audioContentView addTapGestureHandler:^(UITapGestureRecognizer *gestureRecognizer, UIView *view, CGPoint locationInView) {
        if (weakSelf.touchAudioBlock) {
            weakSelf.touchAudioBlock();
        }
    }];
    
    self.playImageView.image = [UIImage imageNamed:@"voice"];
    self.backgroundColor = [UIColor colorWithHex:0xeae0d6];
    
    self.activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
    [self.audioContentView addSubview:self.activityView];
    
    [self.activityView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.audioContentView).offset(20);
        make.centerY.equalTo(self.audioContentView);
        make.height.mas_equalTo(40);
        make.width.mas_equalTo(50);
    }];
}

- (void)updateHeaderInfo:(SRHomeworkDetailItemModel *)item {
    [self.teacherIconImageView fz_setAvatarImageWithUrlString:item.avatar];
    self.teacherNameLabel.text = item.nickname;
    self.createTimeLabel.text = [NSString stringWithFormat:@"发布：%@", [NSDate dateToDisplayStringForTimeInterval:[item.create_time longLongValue]]];
                                 
//    [self.pageImageView fz_setImageWithDefaultPlaceholderWithURLString:item.page_url];
//    self.titleLabel.text = item.title;
    
    
}

- (void)updateQuestionInfo:(SRHomeworkPictureModel *)item {
    [self.pageImageView fz_setImageWithDefaultPlaceholderWithURLString:item.pic];
    self.titleLabel.text = item.desc;
    
    CGFloat maxWidth = SCREEN_WIDTH - 35;
    CGSize maxSize = [item.desc textSizeWithFont:[UIFont systemFontOfSize:13.f] constrainedToSize:CGSizeMake(maxWidth, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];
    if (maxSize.height < 32) {
            //小于两行，隐藏扩展按钮
        if (self.didChangeDescHeightBlock) {
            self.didChangeDescHeightBlock(0);
        }
    } else {
        if (self.didChangeDescHeightBlock) {
            self.didChangeDescHeightBlock(maxSize.height - 32);
        }
    }
    
    if (item.audio && [item.audio length] > 0) {
        self.audioContentView.hidden = NO;
        long long timeStamp = [item.audio_timelen longLongValue];
        if (timeStamp <= 60) {
            self.timeLengthLabel.text = [NSString stringWithFormat:@"%02lld秒", timeStamp];
        } else {
            self.timeLengthLabel.text = [NSString stringWithFormat:@"%02lld分%02lld秒", timeStamp / 60, timeStamp % 60];
        }
    } else {
        self.audioContentView.hidden = YES;
    }

}

- (void)setActivityAnimated:(BOOL)isAnimated {
    
    if (isAnimated) {
        [self.activityView startAnimating];
    } else {
        [self.activityView stopAnimating];
    }
}

@end
